package com.xm.xmapp;

import java.util.ArrayList;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.xm.xmapp.adapter.FragmentStatePagerAdapter;
import com.xm.xmapp.adapter.MyPagerAdapters;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.fragment.CarFeedBackFragment;
import com.xm.xmapp.fragment.PileFeedBackFragment;
import com.xm.xmapp.tools.AndroidTool;
import com.xm.xmapp.tools.ScreenUtils;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.Tools;

public class FeedbackActivity extends BaseActivity {
	private ViewPager vp;
	private MyPagerAdapters adapter;
	private CarFeedBackFragment carFeedBackFragment;
	private PileFeedBackFragment pileFeedBackFragment;
	private TabLayout tab;
	public static int selectFlag = CodeConstant.CarTakePhoto;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback);
	}

	@Override
	public void initView() {
		vp = (ViewPager) findViewById(R.id.vp);
		tab = (TabLayout) findViewById(R.id.tab);
	}

	@Override
	public void initData() {

		setMyTitle("问题反馈");
		ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
		carFeedBackFragment = new CarFeedBackFragment();
		carFeedBackFragment.setProblem_type(1);

		pileFeedBackFragment = new PileFeedBackFragment();
		pileFeedBackFragment.setProblem_type(2);

		fragmentList.add(carFeedBackFragment);
		fragmentList.add(pileFeedBackFragment);
		String[] title = { "车辆", "电桩" };
		adapter = new MyPagerAdapters(title,getSupportFragmentManager(), fragmentList);
		vp.setAdapter(adapter);
		vp.setCurrentItem(0);
		vp.setOffscreenPageLimit(2);
		tab.setupWithViewPager(vp);
		tab.post(new Runnable() {
			@Override
			public void run() {
				int marig = Tools.dp2px(10);
				AndroidTool.setIndicator(tab,marig,marig);
			}
		});

	}


	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		ScreenUtils.hideSoftKeyboard(this);
		return super.dispatchTouchEvent(ev);
	}

	@Override
	public void initEvent() {

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			Bundle bundle = null;
			if (data != null)
				bundle = data.getExtras();

			if (bundle != null && bundle.getString("result") != null) {
				String result = bundle.getString("result");
				if (result.startsWith(CodeConstant.HTML) || result.startsWith(CodeConstant.THTML)) {

					JSONObject htmlResponse = StringTools.getObject(result);
					try {
						int flag = bundle.getInt("flag");
						switch (flag) {
						case 2:
							carFeedBackFragment.setResult(htmlResponse);
							break;
						case 3:
							pileFeedBackFragment.setResult(htmlResponse);
							break;
						default:
							break;
						}

					} catch (Exception e) {
						e.printStackTrace();
						Tools.showMessage("二维码错误");
					}

				} else {
					Tools.showMessage("二维码错误");
				}

			} else {

				switch (selectFlag) {
				case CodeConstant.CarTakePhoto:
					carFeedBackFragment.addImage();
					break;
				case CodeConstant.PileTakePhoto:
					pileFeedBackFragment.addImage();
					break;
				}
			}
		}
	}


}
